package com.demohomework.homeworkdemo.service.impl;

import com.demohomework.homeworkdemo.mapper.AssAllocationMapper;
import com.demohomework.homeworkdemo.mapper.AssSubmitMapper;
import com.demohomework.homeworkdemo.pojo.AssAllocation;
import com.demohomework.homeworkdemo.service.AssAllocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AssAllocationServiceImpl implements AssAllocationService {
    @Autowired
    private AssAllocationMapper assAllocationMapper;
    @Autowired
    private AssSubmitMapper assSubmitMapper;
    @Override
    public List<AssAllocation> list(int stuId, int assClass) {
        return assAllocationMapper.list(stuId, assClass);
    }

    @Override
    public void add(AssAllocation assAllocation) {
        assAllocationMapper.add(assAllocation);
    }

    @Override
    public void save(AssAllocation task) {
        assAllocationMapper.save(task);
    }

    @Override
    public void update(AssAllocation assAllocation) {
        assAllocationMapper.update(assAllocation);
    }

    public void updateAverageScore(int assId) {
        List<Integer> scores = assAllocationMapper.getScoresByAssId(assId);
        if (scores.size() == 5) { // 检查记录条数
            double average = scores.stream().mapToInt(Integer::intValue).average().orElse(0);
            // 更新平均分到 ass_submit 表中
            assSubmitMapper.updateAverageScore(assId, average);
        }
    }
}
